﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Models;
using Mail;

namespace WebHoiChoMuaBan.Controllers
{
    public class NewsProductController : PublicController
    {
        public ActionResult Detail(string productTitle)
        {
            int productId;
            HomeController ctrHome = new HomeController();
            if (int.TryParse(Request.QueryString["prid"], out productId))
            {
                if (productId <= 0)
                    return ctrHome.Index();
                loadCommon();
                NewsProductHelper hNews = new NewsProductHelper();
                NewsProduct news = hNews.getNews(productId);
                if (news != null)
                {
                    CategoryHelper hCate = new CategoryHelper();
                    PageType page = (news.Category.PageType != null ? news.Category.PageType : news.Category.ParentCategory.PageType);
                    List<Category> lstCate = hCate.getCates(page.PageId);
                    ViewData["Categories"] = lstCate;
                    ViewData["CurrentCategory"] = hCate.getCate(news.CateId);
                    ViewData["CurrentPage"] = page;
                    ViewData["News"] = news;
                    if (news.NProNeedType.Equals("SE"))
                    {
                        ViewData["NeedSell"] = NewsProductHelper.NEED_SELL;
                    }
                    ViewData["ProvinceId"] = news.ProvId;
                    HashSet<int> viewedNewsProduct = (HashSet<int>)Session["ViewedNewsProduct"];
                    if (!viewedNewsProduct.Contains(news.NProId))
                    {
                        viewedNewsProduct.Add(news.NProId);
                        news.NProViewCount++;
                        hNews.Save();
                    }
                    return View(string.Format("~/Views/Home/{0}_Detail.aspx", page.Template.TemFilePath));
                }
            }
            return ctrHome.Index();
        }

        [HttpPost]
        public void SendEmail(string mailfrom, string name, string memid, string proid)
        {

            NewsProduct currCus = new NewsProductHelper().getNewsProductbyID(proid);
            // Member currMem = new MemberHelper().getMember(int.Parse(memid));
            SendMail mail = new SendMail("smtp.gmail.com", 587, "Liên hệ sản phẩm từ hoichomuaban.com");
            List<string> noidung = new List<string>();
            noidung.Add(name);
            noidung.Add(mailfrom);
            noidung.Add(currCus.NProTitle);
            noidung.Add(currCus.NProId + "");
            string filePath = Server.MapPath("~/Content/Templates/Emails/LienHeSanPham.txt");
            mail.sendmail("ibclubvlu@gmail.com", "vanlang123", currCus.Member.MemEmail, filePath, noidung);
        }



        public RedirectResult AddComment(FormCollection form)
        {
            if (Session["MemId"] != null)
            {
                string content = (string)form["txtComment"];
                string newsIdStr = form["newsId"];
                string comIdReplyStr = form["replyTo"];
                int memberId = (int)Session["MemId"];
                int newsId, comIdReply;
                ChatHelper chat = new ChatHelper();
                List<ChatEmotionDetail> emos = chat.getEmotionPath();
                foreach (ChatEmotionDetail emo in emos)
                {
                    content = content.Replace(emo.notation, "<img src='" + emo.path + "' width='40px' height='40px'/>");
                }
                if (int.TryParse(newsIdStr, out newsId))
                {
                    NewsProductHelper hNews = new NewsProductHelper();
                    if (int.TryParse(comIdReplyStr, out comIdReply))
                        hNews.AddComment(content, newsId, memberId, comIdReply);
                    else hNews.AddComment(content, newsId, memberId, null);
                }
                else ViewData["ErrorMsg"] = "Error";
            }
            return Redirect(Request.UrlReferrer.AbsoluteUri + "#comments");
        }

        [HttpPost]
        public string Like(string proId)
        {
            if (Session["MemId"] == null)
                return "null";
            NewsProduct pro = new NewsProductHelper().getNewsProductbyID(proId);
            pro.NProLike = pro.NProLike + 1;
            bool isSuccess = new NewsProductHelper().SaveNewsProduct(pro, Session["MemId"] + "");
            if (isSuccess)
                return pro.NProLike + "";
            else
                return "duplicated";

        }

        [HttpPost]
        public string SaveNewsProductMember(string newId)
        {
            if (Session["MemId"] == null)
            {
                string savedNews = Session["MemberSavedNewsProduct"] + "";
                string[] newsid = savedNews.Split('@');
                for (int i = 0; i < newsid.Length; i++)
                {
                    if (!newsid[i].Equals(""))
                    {
                        if(newsid[i].Equals(newId))
                        {
                            return "false";
                        }
                    }
                }
                Session["MemberSavedNewsProduct"] += '@' + newId;
                return "true";
            }
            else
            {
                bool isSuccess = new NewsProductHelper().SaveMemberSaveNewsProduct(Session["MemId"].ToString(), newId);
                if (isSuccess)
                    return "true";
                else
                    return "false";
            }
        }
    }
}
